package com.valyrian.game;

import javax.swing.Renderer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.sun.org.apache.xerces.internal.impl.dtd.models.CMAny;

public class TiledMapGame implements Screen,InputProcessor {

	private static final float CAMERAMOV = 10;
	private TiledMap map;
	private OrthogonalTiledMapRenderer renderer;
	private OrthographicCamera camera; 	
		

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		//camera.position.set(0, 0, 0);
		//renderer.setView(camera);
//		camera.position.set(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 0);
//		camera.update();
		//renderer.setView(camera.combined,camera.position.x, camera.position.y, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		renderer.setView(camera.combined,camera.position.x-Gdx.graphics.getWidth()/2, camera.position.y-Gdx.graphics.getHeight()/2, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		renderer.render();
	}

	@Override
	public void resize(int width, int height) {
		camera.viewportWidth = width;
		camera.viewportHeight = height;
		camera.update();
		
	}

	@Override
	public void show() {
		
		System.out.println("SE INICIO EL JUEGO");
		
		Gdx.input.setInputProcessor(this);
		camera = new OrthographicCamera();
		camera.position.x = Gdx.graphics.getWidth()/2;
		camera.position.y = Gdx.graphics.getHeight()/2;
		map = new TmxMapLoader().load("maps/level04.tmx");
		renderer = new OrthogonalTiledMapRenderer(map);
		
		
		
	}

	@Override
	public void hide() {
		dispose();
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		map.dispose();
		renderer.dispose();
	}

	@Override
	public boolean keyDown(int keycode) {
		switch(keycode){
			case Keys.W:
				camera.position.set(camera.position.x, camera.position.y+CAMERAMOV, camera.position.z);
				break;
			case Keys.A:
				camera.position.set(camera.position.x-CAMERAMOV, camera.position.y, camera.position.z);
				break;
			case Keys.S:
				camera.position.set(camera.position.x, camera.position.y-CAMERAMOV, camera.position.z);
				break;
			case Keys.D:
				camera.position.set(camera.position.x+CAMERAMOV, camera.position.y, camera.position.z);
				break;
			default: 
				return false;
		}
		System.out.println("POSICION DE LA CAMARA: (" + camera.position.x +", "+camera.position.y +", "+camera.position.z +")");
		camera.viewportWidth = Gdx.graphics.getWidth();
		camera.viewportHeight =Gdx.graphics.getHeight();
		camera.update();
	 
		return true;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}
	
	
}
